const router = require('koa-router')()
//生产管理模块
//待分派-待生产员处理中-申请原料中-生产中-入库中-已完成
router.prefix('/produce')
//1.修改订单
router.put('/', async (ctx, next) => {
    let id = ctx.request.body.productId;
    let mtdetail = ctx.request.body.mtdetail;
    let posttime = ctx.request.body.posttime;
    let deadline = ctx.request.body.deadline;
    let sql2 = `update produce set product_mtdetail = '${mtdetail}',product_posttime = '${posttime}',product_deadline = '${deadline}' WHERE product_id = ${id}`;
    await ctx.db.EXCUTE(sql2, []);
    let obj = {
        msg: "编辑成功",
        status: 200
    }
    ctx.body = JSON.stringify(obj);
})

//2.查找订单
router.get('/', async (ctx, next) => {
    let plannerId = ctx.request.query.plannerId;
    let producerId = ctx.request.query.producerId;
    let countSql = "";
    if (plannerId && producerId) {
        countSql = `select count(*) from produce where product_planid = ${plannerId} and product_prid = ${producerId}`;
    } else if (plannerId) {
        countSql = `select count(*) from produce where product_planid = ${plannerId}`;

    } else if (producerId) {
        countSql = `select count(*) from produce where product_prid = ${producerId}`;

    } else {
        countSql = `select count(*) from produce`;
    }
    let countResults = await ctx.db.EXCUTE(countSql, []);
    let total = countResults[0]["count(*)"];
    let pageSize = Number(ctx.request.query.size)
    let page = ctx.request.query.page
    let startRow = total > 0 ? ((page - 1) * pageSize) : 0;
    //实现分页限制查找
    let limitSql;
    if (plannerId && producerId) {
        limitSql = `select * from produce where product_planid = ${plannerId} and product_prid = ${producerId} order by product_id desc limit ${startRow},${pageSize}`;
    } else if (plannerId) {
        limitSql = `select * from produce where product_planid = ${plannerId} order by product_id desc limit ${startRow},${pageSize}`;
    } else if (producerId) {
        limitSql = `select * from produce where product_prid = ${producerId} order by product_id desc limit ${startRow},${pageSize}`;
    } else {
        limitSql = `select * from produce order by product_id desc limit ${startRow},${pageSize}`;
    }
    let results = await ctx.db.EXCUTE(limitSql, []);
    for (let i = 0; i < results.length; i++) {
        let sql = `select user_name from users where user_id = '${results[i].product_planid}'`;
        let res = await ctx.db.EXCUTE(sql, []);
        let plannerName = res[0].user_name;
        results[i].plannerName = plannerName;
        if (results[i].product_prid) {
            let sql2 = `select user_name from users where user_id = '${results[i].product_prid}'`;
            let res2 = await ctx.db.EXCUTE(sql2, []);
            let producerName = res2[0].user_name;
            results[i].producerName = producerName;
        }

    }
    let obj = {
        length: total,
        results: results,
        status: 200
    }
    ctx.body = JSON.stringify(obj);
})

//3.委派生产订单
router.post('/distribution', async (ctx, next) => {
    let id = ctx.request.body.productId;
    let producerId = ctx.request.body.producerId;
    //委派订单，更改订单信息
    // let sql = `select user_id from users where user_name = '${producerName}'`;
    // let result = await ctx.db.EXCUTE(sql, []);
    // let producerId = result[0].user_id;
    let sql2 = `update produce set product_prid = ${producerId},product_type = '待生产员处理中' WHERE product_id = ${id}`;
    await ctx.db.EXCUTE(sql2, []);
    let obj = {
        msg: "委派成功",
        status: 200
    }
    ctx.body = JSON.stringify(obj);
})

//4.产品入库申请
router.post('/inRequire', async (ctx, next) => {
    //将ocdetail处理字符串 申请入库
    let id = ctx.request.body.productId;
    let detail = ctx.request.body.ocdetail;
    let kind = "产品入库";
    let sql = "insert into requires (require_kind, require_orid, require_detail) values('" + kind + "','" + id + "','" + detail + "')";
    let sql2 = `update produce set product_type = '入库中' WHERE product_id = ${id}`;
    await ctx.db.EXCUTE(sql, []);
    await ctx.db.EXCUTE(sql2, []);
    let obj = {
        msg: "入库请求成功",
        status: 200
    }
    ctx.body = JSON.stringify(obj);
})

//5.原料提库申请
router.post('/outRequire', async (ctx, next) => {
    let id = ctx.request.body.productId;
    let detail = ctx.request.body.mtdetail;
    let kind = "原料出库";
    let sql = "insert into requires (require_kind, require_orid, require_detail) values('" + kind + "','" + id + "','" + detail + "')";
    let sql2 = `update produce set product_type = '申请原料中' WHERE product_id = ${id}`;
    await ctx.db.EXCUTE(sql, []);
    await ctx.db.EXCUTE(sql2, []);
    let obj = {
        msg: "出库请求成功",
        status: 200
    }
    ctx.body = JSON.stringify(obj);

})

//6.查询车间生产员的下拉列表
router.get('/producers', async (ctx, next) => {
    let sql = `select * from users where user_roleid = (select role_id from roles where role_desc = '车间生产员')`;
    let results = await ctx.db.EXCUTE(sql, []);
    if (results) {
        let obj = {
            status: 200,
            results: results
        }
        ctx.body = JSON.stringify(obj);
    }
})

//7.查询原材料的下拉列表
router.get('/goods', async (ctx, next) => {
    let sql = `select * from goods where goods_type = 'material'`;
    let results = await ctx.db.EXCUTE(sql, []);
    if (results) {
        let obj = {
            status: 200,
            results: results
        }
        ctx.body = JSON.stringify(obj);
    }
})
module.exports = router